package com.yh.db;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;




/*
 * 1.解析mybatis.xml得到SqlSessionFactory对象
 * 2.得到sqlSession对象
 * 
 */
public class SqlSessionFactoryUtil {
	private static SqlSessionFactory sqlSessionFactory=null;
	
	static{
		Reader reader=null;
		InputStream inputstream=null;
		try {
			reader=Resources.getResourceAsReader("mybatis.xml");
			//inputstream=Resources.getResourceAsStream("mybatis.xml");
			sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
			//sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputstream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static SqlSession getSession(){
		return sqlSessionFactory.openSession();
	}
	
	public static void main(String[] args){
		System.out.println(SqlSessionFactoryUtil.getSession());
	}
	

}